#include <string.h>
#include <stdlib.h>
#include <pthread.h>

#include "common.h"
#include "datainit.h"
#include "data_operations.h"
#include "hd_fs_operation.h"

using namespace std;

#include "async_fs_operator.h"
#include "threadpool.h"

Runtime *g_rt;
extern HadoopFSOperation *g_fsops;
extern AsyncFSOperator<HadoopFSOperation, ThreadPool> *g_async_fsops;
extern ThreadPool *g_threadpool;


int
dataserver_init(char *conf)
{
    DINFO("dataserver_init\n");
    g_rt = (Runtime *)zz_malloc(sizeof(Runtime));
    memset(g_rt, 0x0, sizeof(Runtime));
	pthread_mutex_init(&g_rt->st_m, NULL);
	pthread_mutex_init(&g_rt->id_m, NULL);

	if ((g_rt->conf = readconf(conf)) == NULL) {
		DERROR("parse configure file error\n");
		return -1;
	}

    g_rt->cp = init_connpool(g_rt->conf);
	if (g_rt->cp == NULL) {
		DERROR("connpool init error\n");
		pthread_mutex_destroy(&g_rt->st_m);
		pthread_mutex_destroy(&g_rt->id_m);
		return -1;
	}
    
    DataOperations::Init(g_rt->conf);
    //TODO: change to config file
    g_fsops = new HadoopFSOperation("dirtable", "filetable");

    g_threadpool = new ThreadPool();
        
    g_async_fsops = new AsyncFSOperator<HadoopFSOperation, ThreadPool>(*g_fsops, *g_threadpool);
             
    return 0;
}
